<?php
if (!defined('APP_ID')) { die('No direct script access allowed'); }

class Controller_Group extends Controller_Template {
	protected $_template = 'other/master';
	/**
	 * List of groups
	 */
	public function indexAction() {
		$this->template->content = View::factory('group/index')
			->set('group_list', Group::getList())
			->render();
	}

	/**
	 * Insert group
	 */
	public function insertAction($error = -1) {
		if (Input::isPostBack()) {
			$data = array();
			Query::data($data, 'name', Input::post('group_name'), Query::DATA_STRING);
			Query::data($data, 'status_id', Input::post('group_status_id'), Query::DATA_INTEGER);
			Query::data($data, 'insert_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'insert_time',  time(), Query::DATA_INTEGER);

			$new_group_id = Group::insert($data);
			if ($new_group_id) {
				Tools::redirect(BASE_URL . 'group/update/' . $new_group_id . '/-2');
			} else {
				Tools::redirect(CURRENT_URL . '1');
			}
		} else {
			$this->template->content = View::factory('group/insert')
				->set('error', $error)
				->render();
		}
	}

	/**
	 * Update group
	 */
	public function updateAction($id = 0, $error = -1) {
		if (Input::isPostBack()) {
			$data = array();
			Query::data($data, 'name', Input::post('group_name'), Query::DATA_STRING);
			Query::data($data, 'status_id', Input::post('group_status_id'), Query::DATA_INTEGER);
			Query::data($data, 'update_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'update_time',  time(), Query::DATA_INTEGER);

			if (Group::update($id, $data)) {
				Tools::redirect(CURRENT_URL . $id . '/0');
			} else {
				Tools::redirect(CURRENT_URL . $id . '/1');
			}
		} else {
			$this->template->content = View::factory('group/update')
				->set('error', $error)
				->set('info', Group::getInfo($id))
				->render();
		}
	}
	
	/**
	 * Delete group
	 */
	public function deleteAction($id = 0, $error = -1) {
		if (Input::isPostBack()) {
			$data = array();
			Query::data($data, 'status_id', 3, Query::DATA_INTEGER);
			Query::data($data, 'delete_user_id', User::getId(), Query::DATA_INTEGER);
			Query::data($data, 'delete_time',  time(), Query::DATA_INTEGER);

			if (Group::delete($id, $data)) {
				Tools::redirect(CURRENT_URL . $id . '/0');
			} else {
				Tools::redirect(CURRENT_URL . $id . '/1');
			}
		} else {
			$this->template->content = View::factory('group/delete')
				->set('error', $error)
				->set('info', Group::getInfo($id))
				->render();
		}
	}
	
	/**
	 * Access
	 */
	public function accessAction($group_id = 0) {
		$navigation = Navigation::getData();

		if (Input::isPostBack()) {
			$system_value = Input::post('access_system');
			if (!empty($system_value)) {
				Group::grantAccess($group_id);
			} else {
				Group::denyAccess($group_id);
			}
			foreach ($navigation as $controller_id => $controller) {
				$controller_value = Input::post('access_' . $controller_id);
				if (!empty($controller_value)) {
					Group::grantAccess($group_id, $controller_id);
				} else {
					Group::denyAccess($group_id, $controller_id);
				}
				
				if (isset($controller['actions'])) {
					foreach ($controller['actions'] as $action_id => $action) {
						$action_value = Input::post('access_' . $controller_id . '_' . $action_id);
						if (!empty($action_value)) {
							Group::grantAccess($group_id, $controller_id, $action_id);
						} else {
							Group::denyAccess($group_id, $controller_id, $action_id);
						}
					}
				}
			}
			Tools::redirect(CURRENT_URL . $group_id);
		} else {
			$access = array();
			if ($group_id != Config::get('group_admin')) {
				$access = Group::getAccessList($group_id);
			}
			
			$this->template->content = View::factory('group/access')
				->set('group_id', $group_id)
				->set('group_list', Group::getList())
				->set('navigation', $navigation)
				->set('access', $access)
				->render();
		}
	}
}